package lerry.nape;

import flash.display.StageQuality;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.system.System;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.Lib;
import flash.utils.Object;
import lxq.nape.factory.BodyFactory;

import nape.space.Space;
import nape.space.Broadphase;
import nape.phys.Body;
import nape.phys.BodyList;
import nape.phys.BodyType;
import nape.shape.Polygon;
import nape.geom.Vec2;
import nape.util.Debug;
import nape.util.BitmapDebug;
import nape.util.ShapeDebug;
import nape.constraint.PivotJoint;

class EasyNape extends Sprite {
    private var space:Space;
    private var debug:Debug;
    private var hand:PivotJoint;
    private var useHand:Bool;
	private var useb:Bool;
	var sw:Int;
	var sh:Int;
    function new(gx:Float=0,gy:Float=0,drag:Bool=true,useBitmap:Bool=true) {
        super();
		useHand = drag;
		useb = useBitmap;
		space = new Space(new Vec2(gx, gy));
		addEventListener(Event.ADDED_TO_STAGE, added);
		
		if (drag) {
			hand = new PivotJoint(space.world, null, new Vec2(), new Vec2());
			hand.space = space;
			hand.active = false;
			hand.stiff = false;
		}
    }
	function added(e:Event):Void {
		sw = stage.stageWidth;
		sh = stage.stageHeight;
		stage.frameRate = 60;
		useb?debug = new BitmapDebug(stage.stageWidth, stage.stageHeight):debug = new ShapeDebug(stage.stageWidth, stage.stageHeight);
		addChild(debug.display);
		BodyFactory.border(space, sw, sh);
		
		stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		initSpace();
		
		addEventListener(Event.ENTER_FRAME, loop);
	}
	function onMouseDown(e:MouseEvent):Void {
		mdUtil();
		var mp:Vec2 = Vec2.get(stage.mouseX, stage.mouseY);
		if (useHand) {
			var bodies:BodyList = space.bodiesUnderPoint(mp);
			bodies.foreach(function(b:Body):Void {
					if (b.isDynamic()) {
						hand.body2 = b;
						hand.anchor1.set(mp);
						hand.anchor2 = b.worldPointToLocal(mp);
						hand.active = true;
					}
			});
		}
	}
	function onMouseUp(e:MouseEvent):Void {
		muUtil();
		hand.active = false;
	}
	function loop(e:Event):Void {
		space.step(1 / stage.frameRate, 30, 10);
		debug.clear();
		debug.draw(space);
		debug.flush();
		updateGraphic();
		utils();
		if ( hand.active) {
			hand.anchor1.setxy(stage.mouseX, stage.mouseY);
		}
	}

	function updateGraphic():Void {
		var len:UInt = space.bodies.length;
		for (i in 0...len) {
			var body:Body=space.bodies.at(i);
			if (body.isStatic()) {
				continue;
			}
			var graphic:Object=body.userData.graphic;
			if (graphic == null) {
				continue;
			}
			var pos:Vec2;
			if(body.userData.graphicOffset){
				pos = body.localPointToWorld(body.userData.graphicOffset);
			}else{
				pos=body.localPointToWorld(Vec2.get());
			}
			graphic.x = pos.x;
			graphic.y=pos.y;
			graphic.rotation=(body.rotation * 180 / Math.PI) % 360;
			pos.dispose();
		}
	}
	function setGravity(x:Float = 0, y:Float = 0):Void {
		if (space.gravity.x != x) {
			space.gravity.x = x;
		}
		if (space.gravity.y != y) {
			space.gravity.y = y;
		}
	}
	function initSpace():Void { }
	function utils():Void { }
	function mdUtil():Void { }
	function muUtil():Void{}	
}
